<?php
//
namespace app\circle\controller\admin;

use app\circle\model\CircleModel;
use app\circle\model\MemberModel;
use app\circle\model\MemberReportModel;
use app\circle\model\ThemeModel;
use think\Request;
use think\Validate;

class MemberController extends BaseController
{

    private $memberModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->memberModel = new MemberModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        $query      = $this->request->request();
        $memberMobile  = $this->request->param('member_mobile');
        if (!empty($meetingName)) {
            $map['member_mobile'] = ['like', '%' . $memberMobile . '%'];
        }
        $list = $this->memberModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $result = $this->validate($data, [
                'member_name|用户名'  => 'require|unique:circle_member',
                'member_mobile|手机号'  => 'require|unique:circle_member',
                'member_pass|密码' => 'require',
            ]);
            if (true !== $result) {
                $this->error($result);
            }
            $this->memberModel->addData($data);
            $this->success('操作成功！','index');
        }
        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function edit(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            if(empty($data['member_pass'])){
                unset($data['member_pass']);
            }
            $this->memberModel->editData($data);
            $this->success('操作成功！','index');
        }
        $memberId = $this->request->param('member_id/d');
        $info = $this->memberModel->where('member_id',$memberId)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(){
        $memberId = $this->request->param('member_id', 0);
        $memberStatus = $this->request->param('member_status', 0);
        if ($this->memberModel->where('member_id',$memberId)->setField('member_status',1-$memberStatus)) {
            $this->success('操作成功！');
        } else {
            $this->error($this->memberModel->getError());
        }
    }


    public function report()
    {
        $model=new MemberReportModel();
        if ($this->request->isPost()){
            $data=$this->request->param();
            $validate=new Validate([
                ['id','require','请指定回复信息'],
                ['remsg','require','请填写回复信息']
            ]);
            if (!$validate->check($data)){
                $this->error($validate->getError());
            }
            if ($model->editData($data)){
                $this->success('回复成功');
            }else{
                $this->error('回复失败');
            }
        }
        $map=['remsg'=>''];
        if ($this->adminInfo['admin_power']==3){
            $themeModel=new ThemeModel();
            $themeIds=$themeModel->where(['dept_id'=>$this->adminInfo['dept_id']])->column('theme_id');
            $map['theme_id']=['in',$themeIds];
        }
        $list=$model->where($map)->order('create_time desc')->paginate();
        $this->assign('list',$list);
        return $this->fetch();
    }
}
